/*
 * SplitPaneDemo.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.demos.swingset;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.cute4j.swing.CheckBox;
import com.cute4j.swing.Component;
import com.cute4j.swing.Container;
import com.cute4j.swing.ImageIcon;
import com.cute4j.swing.Label;
import com.cute4j.swing.OptionPane;
import com.cute4j.swing.RadioButton;
import com.cute4j.swing.SelectableGroup;
import com.cute4j.swing.SplitPane;
import com.cute4j.swing.Swing;
import com.cute4j.swing.TextField;
import com.cute4j.swing.events.ActionEvent;
import com.cute4j.swing.events.ActionListener;
import com.cute4j.swing.layouts.BorderLayout;
import com.cute4j.swing.layouts.formlayout.CellConstraints;
import com.cute4j.swing.layouts.formlayout.FormLayout;

/**
 * Demonstration of the SplitPane class.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class SplitPaneDemo extends DemoModule {

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new SplitPaneDemo object.
   */
  public SplitPaneDemo() {
    super("SplitPane Demo", "Demonstration of the SplitPane class.");
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected Component buildDemoComponent() {
    Container panel = new Container(new BorderLayout());
    panel.setInsets(2, 2, 2, 2);

    final SplitPane splitPane = new SplitPane();
    String imageName = "resources/images/splitpane/earth.png";
    ImageIcon earthImage = new ImageIcon(loadResource(imageName));
    final Label earthLabel = new Label(earthImage);
    earthLabel.setMinimumSize(20, 20);
    earthLabel.setBorderWidth(Label.BORDER_TYPE_STANDARD);
    earthLabel.setBorderStyle(Label.BORDER_STYLE_SUNKEN);
    imageName = "resources/images/splitpane/moon.png";
    ImageIcon moonImage = new ImageIcon(loadResource(imageName));
    final Label moonLabel = new Label(moonImage);
    moonLabel.setMinimumSize(20, 20);
    moonLabel.setBorderWidth(Label.BORDER_TYPE_STANDARD);
    moonLabel.setBorderStyle(Label.BORDER_STYLE_SUNKEN);
    splitPane.setLeftComponent(earthLabel);
    splitPane.setRightComponent(moonLabel);
    panel.add(splitPane);

    FormLayout layout = new FormLayout(
                                 "p:g, l:p, 4dlu, l:p, 6dlu, r:p, 4dlu, p, p:g",
                                 "10dlu, p, 4dlu, p, 4dlu, p, 14dlu");
    Container bottomPanel = new Container(layout);
    CellConstraints cc = new CellConstraints();
    SelectableGroup sg = new SelectableGroup();
    RadioButton horizontalSplit = new RadioButton("Horizontal split");
    horizontalSplit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          splitPane.setOrientation(SplitPane.HORIZONTAL);
        }
      });
    horizontalSplit.setSelected(true);
    sg.add(horizontalSplit);
    bottomPanel.add(horizontalSplit, cc.xy(2, 2));
    RadioButton verticalSplit = new RadioButton("Vertical split");
    verticalSplit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          splitPane.setOrientation(SplitPane.VERTICAL);
        }
      });
    sg.add(verticalSplit);
    bottomPanel.add(verticalSplit, cc.xy(2, 4));
    final CheckBox continuousLayout = new CheckBox("Continuous layout");
    continuousLayout.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          splitPane.setContinuousLayout(continuousLayout.isSelected());
        }
      });
    continuousLayout.setSelected(true);
    bottomPanel.add(continuousLayout, cc.xy(4, 2));
    final CheckBox oneTouchExpandable = new CheckBox("One-touch expandable");
    oneTouchExpandable.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          splitPane.setOneTouchExpandable(oneTouchExpandable.isSelected());
        }
      });
    bottomPanel.add(oneTouchExpandable, cc.xy(4, 4));
    bottomPanel.add(new Label("Divider size"), cc.xy(6, 2));
    bottomPanel.add(new Label("First component's min. size"), cc.xy(6, 4));
    bottomPanel.add(new Label("Second component's min. size"), cc.xy(6, 6));
    final TextField dividerWidth =
                      new TextField(String.valueOf(splitPane.getDividerSize()));
    dividerWidth.setHorizontalAlignment(TextField.RIGHT);
    dividerWidth.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          splitPane.setDividerSize(Integer.parseInt(dividerWidth.getText()));
        } catch (Exception ex) {
          OptionPane.showErrorMessage(splitPane, "Invalid divider size.",
                                      "Error");
        }
      }
    });
    Swing.addPropertyChangeListener(Swing.PROPERTY_STYLE,
                                    new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent e) {
        dividerWidth.setText(String.valueOf(splitPane.getDividerSize()));
      }
    });
    bottomPanel.add(dividerWidth, cc.xy(8, 2));
    final TextField minimumEarthLabelWidth =
        new TextField(String.valueOf(earthLabel.getMinimumSize().getWidth()));
    minimumEarthLabelWidth.setHorizontalAlignment(TextField.RIGHT);
    minimumEarthLabelWidth.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          int minimumSize = Integer.parseInt(minimumEarthLabelWidth.getText());
          earthLabel.setMinimumSize(minimumSize, minimumSize);
        } catch (Exception ex) {
          OptionPane.showErrorMessage(splitPane, "Invalid minimum size.",
                                      "Error");
        }
      }
    });
    bottomPanel.add(minimumEarthLabelWidth, cc.xy(8, 4));
    final TextField minimumMoonLabelWidth =
        new TextField(String.valueOf(moonLabel.getMinimumSize().getWidth()));
    minimumMoonLabelWidth.setHorizontalAlignment(TextField.RIGHT);
    minimumMoonLabelWidth.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          int minimumSize = Integer.parseInt(minimumMoonLabelWidth.getText());
          moonLabel.setMinimumSize(minimumSize, minimumSize);
        } catch (Exception ex) {
          OptionPane.showErrorMessage(splitPane, "Invalid minimum size.",
                                      "Error");
        }
      }
    });
    bottomPanel.add(minimumMoonLabelWidth, cc.xy(8, 6));
    panel.add(bottomPanel, BorderLayout.SOUTH);

    return panel;
  }

// Main Method. ----------------------------------------------------------------

  /**
   * Starts demo as standalone application.
   *
   * @param args the application arguments
   */
  public static void main(String[] args) {
    new SplitPaneDemo().show();
  }

}
